package com.manthano.applogin.management.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.manthano.applogin.authorization.bo.UserRole;
import com.manthano.applogin.management.bo.AppUser;
import com.manthano.applogin.management.dao.AppUserDao;
import com.manthano.applogin.management.data.UserType;

/**
 * @author Dell
 */
public class AppUserDaoImpl implements AppUserDao
{
	private SessionFactory	sessionFactory	= null;

	/**
	 * @param sessionFactory
	 */
	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory = sessionFactory;
	}

	/**
	 * @param newSession
	 * @return Session
	 */
	private Session getSession(boolean newSession)
	{
		Session session = sessionFactory.getCurrentSession();
		if (newSession || session == null)
		{
			session = sessionFactory.openSession();
		}
		return session;
	}

	public AppUser addUser(AppUser appUser)
	{
		Session session = getSession(false);
		appUser.setCreatedOn(new Date());
		appUser.setCreatedBy(UserType.SYSTEM_USER.toString());
		appUser.setModifiedOn(new Date());
		appUser.setModifiedBy(UserType.SYSTEM_USER.toString());
		session.save(appUser);
		return appUser;
	}

	public void addUserRole(String userId, UserRole userRole)
	{
		Session session = getSession(false);
		session.save(userRole);
	}

	public boolean updateUser(AppUser appUser)
	{
		Session session = getSession(false);
		appUser.setModifiedOn(new Date());
		appUser.setModifiedBy(UserType.SYSTEM_USER.toString());
		session.saveOrUpdate(appUser);
		return true;
	}

	public boolean activateUser(String userId)
	{
		AppUser appUser = getUser(userId);
		appUser.setModifiedOn(new Date());
		appUser.setModifiedBy(UserType.SYSTEM_USER.toString());
		appUser.setActive(true);
		return true;
	}

	public boolean deactivateUser(String userId)
	{
		AppUser appUser = getUser(userId);
		appUser.setModifiedOn(new Date());
		appUser.setModifiedBy(UserType.SYSTEM_USER.toString());
		appUser.setActive(false);
		return true;
	}

	public boolean removeUser(String userId)
	{
		AppUser appUser = getUser(userId);
		appUser.setModifiedOn(new Date());
		appUser.setModifiedBy(UserType.SYSTEM_USER.toString());
		appUser.setArchive(true);
		return true;
	}

	public AppUser getUser(String userId)
	{
		Session session = getSession(false);
		Criteria criteria = session.createCriteria(AppUser.class);
		criteria = criteria.add(Restrictions.eq("userId", userId));
		return (AppUser) criteria.list().get(0);
	}

	public AppUser getUserByUserName(String userName)
	{
		Session session = getSession(false);
		Criteria criteria = session.createCriteria(AppUser.class);

		Criterion usernameCriterion = Restrictions.eq("userName", userName);
		Criterion emailCriterion = Restrictions.eq("emailAddress", userName);

		criteria.add(Restrictions.or(usernameCriterion, emailCriterion));
		return (AppUser) criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<UserRole> getUserRoles(String userId)
	{
		Session session = getSession(false);
		Criteria criteria = session.createCriteria(UserRole.class);
		criteria = criteria.add(Restrictions.eq("userId", userId));

		return criteria.list();
	}
}
